<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2017/8/12
 * Time: 14:18
 */
    require_once ('../SQLBase.php');
    require_once ('../LoanFee/LoanFee.php');

    class BusinessApp{

        //作废之前的审批
        //1作废普通审批
        //2作废关联借款审批
        //3作废关联借车审批
        function disposeBussinessApprove($appID){
            if($appID == ''){
                return;
            }
            $conn = new SQLBase();
            $conn->queryData("SET AUTOCOMMIT = 0");
            $conn->queryData("BEGIN");

            $sql = "UPDATE travel_approval SET is_dispose = 1 WHERE expense_claim_ID = '$appID'";

            $r = $conn->queryData($sql);


            $sql = "UPDATE loan_car SET is_dispose = 1 WHERE approval_ID = '$appID'";

            $r = $conn->queryData($sql);

            $sql = "UPDATE loan_money SET is_dispose = 1 WHERE approval_ID = '$appID'";
            $r = $conn->queryData($sql);

            if(!$r){
                $conn->queryData('ROLLBACK');
                $conn->queryData("SET AUTOCOMMIT = 1");
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }
            else{
                $conn->queryData('COMMIT');
                $conn->queryData("SET AUTOCOMMIT = 1");
            }
        }


        //新建出差审批，接受审批数据
        function createBusinessApprove($approvalData){
            $conn = new SQLBase();

            //创建一个唯一ID
            $id = uniqid();

            //作废之前的审批
            $this->disposeBussinessApprove($approvalData['prev_app_id']);

            $userStr= "";
            //构造同行人数据
            for($i = 0; $i < count($approvalData['user']); $i++){
                $userStr .= ($approvalData['user'][$i]['departVal'].'-'.$approvalData['user'][$i]['userVal'].';');
            }


            //获取目的地
            $destinantion = $approvalData['destination'];
            //构造目的地数据：省/市/区/具体县乡
            $desStr= $destinantion['province'].'/'.$destinantion['city'].'/'.$destinantion['county'].'/'.$destinantion['detail-county'];

            //计算出差时间，四舍五入
            $days = round((strtotime($approvalData['endTime']) - strtotime($approvalData['startTime'])) / 86400);

            //插入数据
            $query = "INSERT travel_approval(expense_claim_ID, section_ID, write_ID, companion_ID, travel_type, event_ID, travel_destination, travel_start_time,
             travel_end_time, travel_days, self_drive, travel_reason, write_time) VALUES('$id',"."'".$approvalData['user'][0]['departVal']."','".
                $approvalData['user'][0]['userVal']."','".$userStr."','".$approvalData['business_type']."','".
                $approvalData['major_case']."','".$desStr."','".$approvalData['startTime']."','".
                $approvalData['endTime']."','".$days."','".$approvalData['driveSelf']."','".$approvalData['reason']."',NOW())";


            $conn->queryData('SET AUTOCOMMIT=0');
            $conn->queryData("BEGIN");
            $result = $conn->queryData($query);
            //根据插入情况，构造返回数据
            if($result){
                $state = true;
                if($approvalData['isLoanMoney'] != 'false'){
                    $l_fee = new LoanFee();
                    $state = $l_fee->createLoanMoneyApproval($id, $approvalData['loanMoneyData']);
                }
                if($approvalData['isLoanCar'] != 'false'){
                    $state = $this->createLoanCarApproval($id, $approvalData['loanCarData'], $conn);
                }
                if(!$state){
                    $conn->queryData('ROLLBACK');
                    $conn->queryData("SET AUTOCOMMIT=1");
                    return false;
                }
                else{
                    $conn->queryData('COMMIT');
                    $conn->queryData("SET AUTOCOMMIT=1");
                    return $id;
                }
            }
            else{
                $conn->queryData('ROLLBACK');
                $conn->queryData("SET AUTOCOMMIT=1");
                return false;
            }

        }

        //获取某个用户的所有出差审批数据,用户ID，状态，是否是第一次获取
        function getUserBusinessApprove($userid, $state, $firstSet, $pageNum){

            $conn = new SQLBase();
            $query = "SELECT travel_approval.*, allsection.section_name, allsection.section_type, 
                      user.user_name, user.card_number, user.executive_level FROM travel_approval INNER JOIN allsection, user
                      WHERE travel_approval.section_ID = allsection.section_ID AND write_ID='$userid' 
                      AND user.user_ID = travel_approval.write_ID AND allsection.section_state = 1 AND is_dispose = 0";

            //如果不是全部状态，则为0全部,1待审批，2已同意 3已驳回
            if($state != 4){
                $query .= " AND state='$state' ORDER BY write_time DESC";
            }//否则查询全部
            else{
                $query .= " ORDER BY write_time DESC";
            }

            //如果不是要看总数量，则限制获取
            if($firstSet == false){
                $startID = ($pageNum-1) * 10;
                $query .= " LIMIT $startID, 10";
            }

            $result = $conn->queryData($query);
            $result_array = array();
            if($result) {
                $result_array['state'] = 1;
                $result_array['data'] = array();
                if ($firstSet == 1)
                    $result_array['totalCount'] = $result->num_rows;
                else {
                    $result_array['totalCount'] = 0;
                }
                for ($i = 0; $i < $result->num_rows; $i++) {
                    $row = $result->fetch_array();
                    $result_array['data'][$i] = array(
                        'approval_ID'=>$row['expense_claim_ID'],
                        'section_ID'=>$row['section_ID'],
                        'approval_userID'=>$row['write_ID'],
                        'approval_user_name'=>$row['user_name'],
                        'type'=>$row['travel_type'],
                        'destination'=>$row['travel_destination'],
                        'approve_time'=>$row['write_time'],
                        'state'=>$row['state'],
                        'case_ID'=>$row['event_ID'],
                        'state_detail'=>$row['state_detail'],
                        );
                    $eID = $row['event_ID'];
                    if(!empty($eID)){
                        $q = "SELECT section_name as case_name FROM allsection WHERE section_ID='$eID'";
                        $r = $conn->queryData($q);
                        $result_array['data'][$i]['case_name'] = $r->fetch_array()['case_name'];
                    }
                    else{
                        $result_array['data'][$i]['case_name'] = '';
                    }
                    //获取相关借车信息
                    $r = $this->getLoanCarInfo($row['expense_claim_ID']);
                    if($r){
                        $result_array['data'][$i]['loan_car'] = $r;
                        $result_array['data'][$i]['is_loan_car'] = 1;
                    }
                    else{
                        $result_array['data'][$i]['loan_car'] = '';
                        $result_array['data'][$i]['is_loan_car'] = 0;
                    }
                    //获取相关借款信息
                    $l_fee = new LoanFee();
                    $r = $l_fee->getLoanMoneyInfo($row['expense_claim_ID']);
                    if($r){
                        $result_array['data'][$i]['loan_money'] = $r;
                        $result_array['data'][$i]['is_loan_money'] = 1;
                    }
                    else{
                        $result_array['data'][$i]['loan_money'] = '';
                        $result_array['data'][$i]['is_loan_money'] = 0;
                    }
                }
            }
            else{
                $result_array['state'] = 0;
            }
            return $result_array;

        }

        //辅助函数，获取出差审批单详细信息
        //1 获取基础审批单
        //2 获取关联借车审批单
        //3 获取关联借款审批单
        function getBusinessApproveDetail($appID){
            $result_array = $this->getBusinessInfoByAppID($appID);
            $car_result = $this->getLoanCarInfo($appID);
            if($car_result){
                $result_array['data'][0]['loan_car'] = $car_result;
                $result_array['data'][0]['is_loan_car'] = 1;
            }
            else{
                $result_array['data'][0]['loan_car'] = '';
                $result_array['data'][0]['is_loan_car'] = 0;
            }
            $l_fee = new LoanFee();
            $money_result = $l_fee->getLoanMoneyInfo($appID);
            if($money_result){
                if($money_result['state'] == 2){
                    $result_array['data'][0]['is_loan_money'] = 0;
                    $result_array['data'][0]['loan_money'] = 0;
                }
                else{
                    $result_array['data'][0]['loan_money'] = $money_result;
                    $result_array['data'][0]['is_loan_money'] = 1;
                }
            }
            else{
                $result_array['data'][0]['loan_money'] = '';
                $result_array['data'][0]['is_loan_money'] = 0;
            }


            return $result_array;
        }

        //根据出差审批单ID获取详细信息
        function getBusinessInfoByAppID($appID){
            $query = "SELECT travel_approval.*, allsection.section_name, user.user_name, user.card_number, 
                      user.executive_level FROM travel_approval INNER JOIN allsection, user
                      WHERE travel_approval.section_ID = allsection.section_ID AND travel_approval.expense_claim_ID = '$appID' 
                      AND user.user_ID = travel_approval.write_ID";


            $conn = new SQLBase();
            $result = $conn->queryData($query);
            if($result){
                $result_array['state'] = 1;
                $result_array['data'] = array();

                for($i = 0; $i < $result->num_rows; $i++){
                    $row = $result->fetch_array();
                    $comp = explode(';', $row['companion_ID']);
                    $comArray = array();
                    for($j = 0; $j < count($comp) - 1; $j++){
                        $t = explode('-', $comp[$j]);
                        $query = "SELECT user_name FROM user WHERE user_ID='".$t[1]."'";
                        $r_user = $conn->queryData($query);
                        $username = $r_user->fetch_array()['user_name'];

                        $query = "SELECT section_name FROM allsection WHERE section_id='".$t[0]."'";
                        $r_section = $conn->queryData($query);
                        $departname = $r_section->fetch_array()['section_name'];
                        $comArray[$j] = array('comp_user_id'=> $t[1], 'comp_user_name'=>$username, 'comp_depart_id'=>$t[0], 'comp_depart_name'=>$departname);
                    }
                    $result_array['data'][$i] = array('approval_ID'=>$row['expense_claim_ID'],
                        'section_ID'=>$row['section_ID'], 'approval_userID'=>$row['write_ID'], 'executive_level'=>$row['executive_level'],
                        'section_type'=>$row['section_type'],
                        'approval_user_name'=>$row['user_name'], 'approval_user_card'=>$row['card_number'],
                        'companion'=>$comArray, 'type'=>$row['travel_type'],
                        'destination'=>$row['travel_destination'], 'start_time'=>$row['travel_start_time'],
                        'end_time'=>$row['travel_end_time'], 'days'=>$row['travel_days'],
                        'self_drive'=>$row['self_drive'], 'reason'=>$row['travel_reason'],
                        'files'=>$row['with_file'], 'operator'=>$row['operator_ID'],
                        'section_manager_ID'=>$row['section_manager_ID'],
                        'section_leader_ID'=>$row['section_leader_ID'],
                        'leader_ID'=>$row['leader_ID'],
                        'approve_time'=>$row['write_time'],
                        'section_name'=>$row['section_name'],
                        'state'=>$row['state'], 'case_ID'=>$row['event_ID'], 'state_detail'=>$row['state_detail'],
                        'section_approve_opinion'=>$row['section_approve_opinion'],
                        'section_leader_opinion'=>$row['section_leader_opinion'],
                        'leader_opinion'=>$row['leader_opinion']);

                    $eID = $row['event_ID'];

                    if(!empty($eID)){
                        $q = "SELECT section_name as case_name FROM allsection WHERE section_ID='$eID'";
                        $r = $conn->queryData($q);
                        $result_array['data'][$i]['case_name'] = $r->fetch_array()['case_name'];
                    }
                    else{
                        $result_array['data'][$i]['case_name'] = '';
                    }
                }
            }
            else
                $result_array['state'] = 0;
            return $result_array;
        }

//        //创建借款审批单
//        function createLoanMoneyApproval($appID, $loanMoneyData, $conn){
//            $id = uniqid();
//            $query = "INSERT loan_money(loan_money_ID, user_ID, section_ID, approval_ID, loan_money, loan_money_reason, write_time)
//                     VALUES('$id','".$loanMoneyData['user_id']."','".$loanMoneyData['depart_id']."','".$appID."','".$loanMoneyData['money']."','".$loanMoneyData['reason']."',NOW())";
//            $r = $conn->queryData($query);
//            if($r){
//                return $id;
//            }
//            else{
//                return false;
//            }
//        }

        //创建借车审批单
        function createLoanCarApproval($appID, $loanCarData, $conn){
            $id = uniqid();
            $query = "INSERT loan_car(loan_car_ID, user_ID, section_ID, approval_ID, loan_car_reason, loan_car_driver, driver_name, loan_car_num, write_time)
                     VALUES('$id','".$loanCarData['user_id']."','".$loanCarData['depart_id']."','".$appID."','".$loanCarData['reason']."','".$loanCarData['driverID']."','".$loanCarData['driverName']."','".$loanCarData['carNum']."',NOW())";
            $r = $conn->queryData($query);
            if($r){
                return $id;
            }
            else{
                return false;
            }
        }

        //获取借车审批信息
        function getLoanCarInfo($appID){
            $conn = new SQLBase();
            $query = "SELECT loan_car.*, user.user_name, allsection.section_name FROM loan_car INNER JOIN user, allsection WHERE loan_car.approval_ID='$appID' 
                  AND loan_car.user_ID = user.user_ID AND loan_car.section_ID = allsection.section_ID AND is_dispose = 0";
            $r = $conn->queryData($query);
            if($r){
                if($r->num_rows > 0){
                    $row = $r->fetch_array();
                    $loanCarData = array('loan_car_id'=>$row['loan_car_ID'],
                        'user_id'=>$row['user_ID'],
                        'user_name'=>$row['user_name'],
                        'depart_id'=>$row['section_ID'],
                        'section_name'=>$row['section_name'],
                        'approval_ID'=>$row['approval_ID'],
                        'reason'=>$row['loan_car_reason'],
                        'driverID'=>$row['loan_car_driver'],
                        'driverName'=>$row['driver_name'],
                        'carNum'=>$row['loan_car_num'],
                        'operator_ID'=>$row['operator_ID'],
                        'section_manager_ID'=>$row['section_manager_ID'],
                        'section_leader_ID'=>$row['section_leader_ID'],
                        'leader_ID'=>$row['leader_ID'],
                        'write_time'=>$row['write_time'],
                        'state'=>$row['state'],
                        'state_detail'=>$row['state_detail'],
                        'section_approve_opinion'=>$row['section_approve_opinion'],
                        'section_leader_opinion'=>$row['section_leader_opinion'],
                        'leader_opinion'=>$row['leader_opinion']);
                    $query = "SELECT user.user_name FROM user WHERE user.user_ID = '".$row['loan_car_driver']."'";
                    $r = $conn->queryData($query);
                    if($r->num_rows > 0){
                        $loanCarData['driver_name'] = $r->fetch_array()['user_name'];
                    }
                    else{
                        $loanCarData['driver_name'] = '';
                    }
                    return $loanCarData;
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
        }

        //获取同行人信息
        function getTravelPartner($appID){
            $query = "SELECT companion_ID FROM travel_approval WHERE expense_claim_ID = '$appID'";
            $conn = new SQLBase();
            $result = $conn->queryData($query);
            if($result){
                for($i = 0; $i < $result->num_rows; $i++) {
                    $row = $result->fetch_array();
                    $comp = explode(';', $row['companion_ID']);
                    $comArray = array();
                    for ($j = 0; $j < count($comp) - 1; $j++) {
                        $t = explode('-', $comp[$j]);
                        $query = "SELECT user_name FROM user WHERE user_ID='" . $t[1] . "'";
                        $r_user = $conn->queryData($query);
                        $username = $r_user->fetch_array()['user_name'];

                        $query = "SELECT section_name FROM allsection WHERE section_id='" . $t[0] . "'";
                        $r_section = $conn->queryData($query);
                        $departname = $r_section->fetch_array()['section_name'];
                        $comArray[$j] = array('comp_user_id' => $t[1], 'comp_user_name' => $username, 'comp_depart_id' => $t[0], 'comp_depart_name' => $departname);
                    }
                }
                return $comArray;
            }
            else{
                return false;
            }

        }

//        //获取借款审批信息
//        function getLoanMoneyInfo($appID, $byApp = true){
//            $conn = new SQLBase();
//            if($byApp)
//                $query = "SELECT loan_money.*, user.user_name, allsection.section_name FROM loan_money INNER JOIN user, allsection WHERE loan_money.approval_ID='$appID'
//                      AND loan_money.user_ID = user.user_ID AND loan_money.section_ID = allsection.section_ID";
//            else
//                $query = "SELECT loan_money.*, user.user_name, allsection.section_name FROM loan_money INNER JOIN user, allsection WHERE loan_money.loan_money_ID='$appID'
//                      AND loan_money.user_ID = user.user_ID AND loan_money.section_ID = allsection.section_ID";
//            $r = $conn->queryData($query);
//            if($r){
//                if($r->num_rows > 0){
//                    $row = $r->fetch_array();
//                    $loanMoneyData = array('loan_money_id'=>$row['loan_money_ID'],
//                        'user_id'=>$row['user_ID'],
//                        'user_name'=>$row['user_name'],
//                        'depart_id'=>$row['section_ID'],
//                        'section_name'=>$row['section_name'],
//                        'approval_ID'=>$row['approval_ID'],
//                        'reason'=>$row['loan_money_reason'],
//                        'money'=>$row['loan_money'],
//                        'return_money'=>$row['return_money'],
//                        'operator_ID'=>$row['operator_ID'],
//                        'section_manager_ID'=>$row['section_manager_ID'],
//                        'section_leader_ID'=>$row['section_leader_ID'],
//                        'leader_ID'=>$row['leader_ID'],
//                        'write_time'=>$row['write_time'],
//                        'state'=>$row['state'],
//                        'state_detail'=>$row['state_detail'],
//                        'section_approve_opinion'=>$row['section_approve_opinion'],
//                        'section_leader_opinion'=>$row['section_leader_opinion'],
//                        'leader_opinion'=>$row['leader_opinion']);
//                    return $loanMoneyData;
//                }
//                else{
//                    return false;
//                }
//            }
//            else{
//                return false;
//            }
//        }

        //获取部门出差审批信息
        function getTravelApproveBySection($sectionID, $allsection, $state){
            if($allsection == 0)
                $sql = "SELECT travel_approval.*, allsection.section_name, user.user_name FROM travel_approval INNER JOIN allsection, user 
                  WHERE travel_approval.section_ID = '$sectionID' AND travel_approval.section_ID = allsection.section_ID 
                  AND travel_approval.write_ID = user.user_ID AND travel_approval.state_detail='$state' AND travel_approval.state = '0'";
            else{
                $sql = "SELECT travel_approval.*, allsection.section_name, user.user_name FROM travel_approval INNER JOIN allsection, user 
                      WHERE travel_approval.section_ID = allsection.section_ID AND travel_approval.write_ID = user.user_ID 
                      AND travel_approval.state_detail='$state' AND travel_approval.state = '0' AND travel_approval.section_ID IN (";
                for($i = 0; $i < count($sectionID); $i++){
                    $sql .= "'$sectionID[$i]'";
                    if($i != count($sectionID)-1){
                        $sql .= ",";
                    }
                    else{
                        $sql .= ")";
                    }
                }
            }
            $conn = new SQLBase();
            $r = $conn->queryData($sql);
            $result_array = array();
            if($r){
                $result_array['state'] = 1;
                $result_array['state_error'] = '';
                $result_array['data'] = array();
                for($i = 0; $i < $r->num_rows; $i++){
                    $row = $r->fetch_array();
                    $result_array['data'][$i] = array('type'=>1, 'user_name'=>$row['user_name'], 'user_id'=>$row['write_ID'], 'section_id'=>$row['section_ID'], 'travel_type'=>$row['travel_type'], 'app_time'=>$row['write_time'], 'app_id'=>$row['expense_claim_ID'], 'section_name'=>$row['section_name']);
                }
                $result_array['total_count'] = $r->num_rows;
            }
            else{
                $result_array['state'] = 0;
                $result_array['state_error'] = 'DB_ERROR';
            }
            return $result_array;
        }

        //获取部门借款信息
        function getLoanFeeApproveBySection($sectionID, &$result_array, $allsection, $state){
            if($allsection == 0){
                $sql = "SELECT loan_money.*, allsection.section_name, user.user_name FROM loan_money, allsection, user WHERE loan_money.section_ID = '$sectionID' AND loan_money.section_ID = allsection.section_ID 
                  AND loan_money.user_ID = user.user_ID AND loan_money.state_detail='$state' AND loan_money.state = '0' AND loan_money.approval_ID = ''";
            }
            else{
                $sql = "SELECT loan_money.*, allsection.section_name, user.user_name FROM loan_money, allsection, user WHERE loan_money.section_ID = allsection.section_ID 
                  AND loan_money.user_ID = user.user_ID AND loan_money.state_detail='$state' AND loan_money.state = '0' AND loan_money.approval_ID = '' AND loan_money.section_ID IN (";
                for($i = 0; $i < count($sectionID); $i++){
                    $sql .= "'$sectionID[$i]'";
                    if($i != count($sectionID)-1){
                        $sql .= ",";
                    }
                    else{
                        $sql .= ")";
                    }
                }
            }
            $conn = new SQLBase();
            $r = $conn->queryData($sql);
            if($r){
                $result_array['state'] = 1;
                $result_array['state_error'] = '';
                for($i = 0; $i < $r->num_rows; $i++){
                    $row = $r->fetch_array();
                    $t = array('type'=>2, 'user_name'=>$row['user_name'], 'user_id'=>$row['user_ID'], 'section_id'=>$row['section_ID'], 'loan_money'=>$row['loan_money'], 'app_time'=>$row['write_time'], 'app_id'=>$row['loan_money_ID'], 'section_name'=>$row['section_name']);
                    array_push($result_array['data'], $t);
                }
            }
            else{
                $result_array['state'] = 0;
                $result_array['state_error'] = 'DB_ERROR';
            }
        }

        //批准某个出差审批，接受参数为appID,审批人ID，同意/拒绝，哪一级审批，审批内容
        function ApproveBussiness($appID, $userID, $type, $opinion, $opinion_detail){
            $sql = '';
            if($opinion == 1){
                switch($type){
                    case 1:
                        $sql = "UPDATE travel_approval SET section_manager_ID='$userID', state_detail='02', section_approve_opinion='$opinion_detail' WHERE expense_claim_ID='$appID'";
                        break;
                    case 2:
                        if($this->isApproveInArea($appID)){
                            $sql = "UPDATE travel_approval SET section_leader_ID='$userID', state_detail='11', state='1', 
                                    section_leader_opinion='$opinion_detail', leader_ID='$userID', leader_opinion='$opinion_detail' 
                                    WHERE expense_claim_ID='$appID'";
                        }
                        else{
                            $sql = "UPDATE travel_approval SET section_leader_ID='$userID', state_detail='03', section_leader_opinion='$opinion_detail' WHERE expense_claim_ID='$appID'";
                        }
                        break;
                    case 3:
                        $sql = "UPDATE travel_approval SET leader_ID='$userID', state='1', state_detail='11', leader_opinion='$opinion_detail' WHERE expense_claim_ID='$appID'";
                        break;
                }
            }
            else{
                switch($type){
                    case 1:
                        $sql = "UPDATE travel_approval SET section_manager_ID='$userID', state='2', state_detail='21', section_approve_opinion='$opinion_detail' WHERE expense_claim_ID='$appID'";
                        break;
                    case 2:
                        $sql = "UPDATE travel_approval SET section_leader_ID='$userID', state='2', state_detail='22', section_leader_opinion='$opinion_detail' WHERE expense_claim_ID='$appID'";
                        break;
                    case 3:
                        $sql = "UPDATE travel_approval SET leader_ID='$userID', state='2', state_detail='23', leader_opinion='$opinion_detail' WHERE expense_claim_ID='$appID'";
                        break;
                }

            }

            $conn = new SQLBase();

            $r = $conn->queryData($sql);
            if($r){
                return true;
            }else{
                return false;
            }

        }

        //批准某个借车审批，接受参数为appID,审批人ID，同意/拒绝，哪一级审批，审批内容
        function ApproveLoanCar($appID, $userID, $type, $opinion, $opinion_detail){
            $sql = '';
            if($opinion == 1){
                switch($type){
                    case 1:
                        $sql = "UPDATE loan_car SET section_manager_ID='$userID', state_detail='02', section_approve_opinion='$opinion_detail' WHERE approval_ID='$appID'";
                        break;
                    case 2:
                        if($this->isApproveInArea($appID)){
                            $sql = "UPDATE loan_car SET section_leader_ID='$userID', leader_ID='$userID', state='1', leader_opinion='$opinion_detail' 
                                  state_detail='11', section_leader_opinion='$opinion_detail' WHERE approval_ID='$appID'";
                        }
                        else{
                            $sql = "UPDATE loan_car SET section_leader_ID='$userID', state_detail='03', section_leader_opinion='$opinion_detail' WHERE approval_ID='$appID'";
                        }
                        break;
                    case 3:
                        $sql = "UPDATE loan_car SET leader_ID='$userID', state='1', state_detail='11', leader_opinion='$opinion_detail' WHERE approval_ID='$appID'";
                        break;
                }
            }
            else{
                switch($type){
                    case 1:
                        $sql = "UPDATE loan_car SET section_manager_ID='$userID', state='2', state_detail='21', section_approve_opinion='$opinion_detail' WHERE approval_ID='$appID'";
                        break;
                    case 2:
                        $sql = "UPDATE loan_car SET section_leader_ID='$userID', state='2', state_detail='22', section_leader_opinion='$opinion_detail' WHERE approval_ID='$appID'";
                        break;
                    case 3:
                        $sql = "UPDATE loan_car SET leader_ID='$userID', state='2', state_detail='23', leader_opinion='$opinion_detail' WHERE approval_ID='$appID'";
                        break;
                }
            }

            $conn = new SQLBase();

            $r = $conn->queryData($sql);
            if($r){
                return true;
            }else{
                return false;
            }

        }

        //辅助函数，判断当前申请是在区内还是区外
        function isApproveInArea($appID){
            $sql = "SELECT travel_destination FROM travel_approval WHERE expense_claim_ID = '$appID'";
            $conn = new SQLBase();
            $r = $conn->queryData($sql);
            if($r){
                if($r->num_rows > 0){
                    $addr = $r->fetch_array()['travel_destination'];
                    $temp = explode('/', $addr);
                    if($temp[0] == '四川省' && $temp[1] == '宜宾市' && $temp[2] == '翠屏区'){
                        return true;
                    }
                    else{
                        return false;
                    }
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
        }

        //获取建立出差审批PDF的数据
        function getBusinessAppPDFData($appID){
            $conn = new SQLBase();

            $query = "SELECT travel_approval.*, business_type.type_name, allsection.section_name FROM travel_approval INNER JOIN business_type, allsection
              WHERE expense_claim_ID='$appID' AND travel_approval.travel_type = business_type.Id AND travel_approval.section_ID = allsection.section_ID";


            $result = $conn->queryData($query);

            $result_array = array();

            if($result){
                $data = $result->fetch_array();
                $appData = array();
                $appData['appID']=$appID;
                $appData['section_name']=$data['section_name'];
                $appData['reason'] = $data['type_name'];

                $eID = $data['event_ID'];
                if(!empty($eID)){
                    $q = "SELECT section_name FROM allsection WHERE section_ID='$eID'";
                    $r = $conn->queryData($q);

                    $appData['reason'].= "-".$r->fetch_array()['section_name'];
                }
                $appData['reason'].= "\n".$data['travel_reason'];

                $appData['destination'] = $data['travel_destination'];

                $appData['timerange'] = $data['travel_start_time']."到".$data['travel_end_time']."(共".$data['travel_days']."天)";
                $appData['self_drive'] = $data['self_drive'] ? "是" : "否";
                $appData['section_manager'] = $data['section_manager_ID'];
                $appData['section_leader'] = $data['section_leader_ID'];
                $appData['leader'] = $data['leader_ID'];

                $comp = explode(';', $data['companion_ID']);
                for($j = 0; $j < count($comp) - 1; $j++){
                    $t = explode('-', $comp[$j]);
                    $query = "SELECT user_name FROM user WHERE user_ID='".$t[1]."'";
                    $r_user = $conn->queryData($query);
                    $username = $r_user->fetch_array()['user_name'];

                    $query = "SELECT section_name FROM allsection WHERE section_id='".$t[0]."'";
                    $r_section = $conn->queryData($query);
                    $departname = $r_section->fetch_array()['section_name'];
                    $appData['companion'] .= $departname."-".$username.";";
                    if(($j+1) % 3 == 0){
                        $appData['companion'].="\n";
                    }
                }
                $appData['companion'].="(共".(count($comp)-1)."人)";
                //还应该获取签批人信息和对应的签名图片


                return $appData;


            }
            else{
                return false;
            }
        }

        //辅助函数，更改差旅审批单状态
        function changeBusinessAppState($appID, $state){
            $conn = new SQLBase();
            $sql = "UPDATE travel_approval SET state = $state WHERE expense_claim_ID = '$appID' AND is_dispose = 0";
            $r = $conn->queryData($sql);
            if(!$r){
                return false;
            }
            else{
                return true;
            }
        }
    }



?>